package com.tandashi.springbootstudy01.service.realm;

import com.tandashi.springbootstudy01.entity.user.User;
import com.tandashi.springbootstudy01.service.user.UserService;
import com.tandashi.springbootstudy01.service.user.impl.UserServiceImpl;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

public class DefinitonRealm extends AuthorizingRealm {

    /**
     * 认证方法
     * @param token
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        String loginName = (String)token.getPrincipal();
        UserService userService = new UserServiceImpl();
        User user = userService.getUserByUserName(loginName);
        if(user == null){
            throw new UnknownAccountException("账号不存在");
        }
        return new SimpleAuthenticationInfo(loginName,user.getPassword(),getName());
    }

    /**
     * 授权方法
     * @param principals
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        return null;
    }


}
